package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.GoodsDao;
import cn.gjculture.shopping.api.dao.GoodsTypeDao;
import cn.gjculture.shopping.api.entity.Goods;
import cn.gjculture.shopping.api.entity.GoodsType;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.GoodsTypeService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    GoodsTypeDao goodsTypeDao;

    @Autowired
    CommonUpdateMapper commonUpdateDao;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;

    @Autowired
    GoodsDao goodsDao;

    @Override
    public int insert(GoodsType goodstype) {
        goodstype.setGoodsTypeId(UUIDUtil.getUUID());
        goodstype.setInsertTime(DateUtil.getTimestamp());
        goodstype.setUpdateTime(DateUtil.getTimestamp());
        goodstype.setStatus(1);
        return goodsTypeDao.insert(goodstype);
    }

    @Override
    public List<GoodsType> query() {
        List<GoodsType> list=goodsTypeDao.query();
        List<GoodsType> goodsTypes = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (GoodsType g : list) {
                if ("0".equals(g.getParentGoodsTypeId())) {
                    List<GoodsType> goodsTypeList = findSubGoodType(g.getGoodsTypeId(), list);
                    g.setChildren(goodsTypeList);
                    goodsTypes.add(g);
                }
            }
        }
        return goodsTypes;
    }
    private List<GoodsType> findSubGoodType(String goodsTypeId, List<GoodsType> list) {
        List<GoodsType> goodsTypeList = new ArrayList<>();
        for (GoodsType g : list) {
            if (goodsTypeId.equals(g.getParentGoodsTypeId())) {
                goodsTypeList.add(g);
            }
        }
        return goodsTypeList;
    }

    @Override
    public int update(GoodsType goodsType) {
        goodsType.setUpdateTime(DateUtil.getTimestamp());
        return  commonUpdateDao.updateGoodsType(goodsType);
    }

    @Override
    public int deleteMore(List<String> goodsTypeIds, Return ret) {
        if(goodsTypeIds!=null&&goodsTypeIds.size()>0){
            for(String id:goodsTypeIds){
                List<Goods> goodsList=goodsDao.queryByTypeId(id);
                if(!CollectionUtils.isEmpty(goodsList)){
                    ret.getCommon().setMessage("商品分类下面有商品，不能删除");
                    return 0;
                }
                Integer goodsTypes=goodsTypeDao.queryByParentCount(id);
                if (goodsTypes!=null&&goodsTypes>0){
                    ret.getCommon().setMessage("商品分类下面有子分类，不能删除");
                    return 0;
                }
            }
            commonDeleteMapper.deleteGoodsType(goodsTypeIds);
        }
        return 1;
    }
}
